package my.javafx.editor;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.mxGraphOutline;
import com.mxgraph.util.*;
import com.mxgraph.view.mxGraph;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TabPane;

import java.util.List;


/**

 */
public class JavaFxBasicEditor {

    //窗口的标题
    protected String title;

    //所需要的图形存储对象
    protected mxGraphComponent graphComponent;
    //工具栏中编辑操作对象
    protected mxUndoManager undoManager;
    //选项卡
    protected TabPane libraryPane;
    //特定图形组件的大纲视图
    protected mxGraphOutline graphOutline;

    public JavaFxBasicEditor(String title,mxGraphComponent component) {
        //设置窗口的标题
        this.title = title;
        //存储对图形的引用
        graphComponent = component;
        //获取mxGraph对象
        final mxGraph graph = graphComponent.getGraph();
        //文件加载后不改变原有的尺寸
        graph.setResetViewOnRootChange(false);

        //graph.getModel().addListener(mxEvent.CHANGE, changeTracker);

        // 将命令历史记录添加到模型和视图
        graph.getModel().addListener(mxEvent.UNDO, undoHandler);
        graph.getView().addListener(mxEvent.UNDO, undoHandler);

        mxEventSource.mxIEventListener undoHandler = new mxEventSource.mxIEventListener()
        {
            public void invoke(Object source, mxEventObject evt)
            {
                List<mxUndoableEdit.mxUndoableChange> changes = ((mxUndoableEdit) evt
                        .getProperty("edit")).getChanges();
                graph.setSelectionCells(graph
                        .getSelectionCellsForChanges(changes));
            }
        };

        undoManager.addListener(mxEvent.UNDO, undoHandler);
        undoManager.addListener(mxEvent.REDO, undoHandler);

        //创建图形大纲组件
        graphOutline = new mxGraphOutline(graphComponent);

        //创建库窗格
        libraryPane = new TabPane();
        //创建窗格对象
        SplitPane inner = new SplitPane();
        //设置分割线的位置
        inner.setDividerPositions(300);
        //设置分割线的宽度
        inner.setPrefWidth(1);








    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public mxGraphComponent getComponent() {
        return graphComponent;
    }

    public void setComponent(mxGraphComponent component) {
        this.graphComponent = component;
    }
    //创建一个处理撤销历史记录对象
    protected mxUndoManager createUndoManager()
    {
        return new mxUndoManager();
    }

    protected mxEventSource.mxIEventListener undoHandler = new mxEventSource.mxIEventListener()
    {
        public void invoke(Object source, mxEventObject evt)
        {
            undoManager.undoableEditHappened((mxUndoableEdit) evt
                    .getProperty("edit"));
        }
    };

    /*protected mxEventSource.mxIEventListener changeTracker = new mxEventSource.mxIEventListener()
    {
        public void invoke(Object source, mxEventObject evt)
        {
            setModified(true);
        }
    };*/
}
